package com.service.impl;


import com.dao.PermissionRoleResourceMapper;
import com.exception.ServiceRuntimeException;
import com.model.PermissionRoleResource;
import com.po.PermissionRoleResourcePO;
import com.service.PermissionRoleResourceService;
import com.vo.PermissionRoleResourceVO;
import org.apache.logging.log4j.core.util.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chihaojie
 * @Date: 2019/6/3 16:24
 * @Version 1.0
 * @Note
 */
@Service
public class PermissionRoleResourceServiceImpl  implements PermissionRoleResourceService {

    @Autowired
     private    PermissionRoleResourceMapper  permissionRoleResourceMapper;

    @Override
    public Boolean add(PermissionRoleResourcePO permissionRoleResourcePO) {
        Assert.notNull(permissionRoleResourcePO,"参数不能为空");
        PermissionRoleResourceVO result=new  PermissionRoleResourceVO();
        List<Long>  resourceIds=permissionRoleResourcePO.getResourceIds();
        Long  roleId     =permissionRoleResourcePO.getRoleId();
        if (!ObjectUtils.isEmpty(resourceIds)) {
            // 4.保存选中的角色资源关系
            permissionRoleResourceMapper.insertBatch(resourceIds, roleId);
        }
        return true;
    }

    @Override
    public Boolean edit(PermissionRoleResourcePO permissionRoleResourcePO) {
        Assert.notNull(permissionRoleResourcePO,"参数不能为空");
        Assert.notNull(permissionRoleResourcePO.getRoleId(),"id不能为空");
        PermissionRoleResourceVO result   =  new PermissionRoleResourceVO();
        Date   now = new Date();
        //先删除再新增
        permissionRoleResourceMapper.deleteByRoleId(permissionRoleResourcePO.getRoleId());
        List<Long>  resourceIds=permissionRoleResourcePO.getResourceIds();
        Long  roleId     =permissionRoleResourcePO.getRoleId();
        if (!ObjectUtils.isEmpty(resourceIds)) {
            // 4.保存选中的角色资源关系
            permissionRoleResourceMapper.insertBatch(resourceIds, roleId);
        }
        return true;
    }

    @Override
    public Boolean remove(Long roleId) {
        permissionRoleResourceMapper.deleteByRoleId(roleId);
        return true;
    }
}
